﻿using ClassLibrary.Service;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ClassLibrary;
using System.Reflection.Metadata;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace Forms
{
    public partial class AddItem : UIForm
    {
        User user;
        Item newItem = new Item();
        ItemService itemService = new ItemService();
        public AddItem(User user, string marketID)
        {
            SetStyle(
                ControlStyles.UserPaint//使用自定义的绘制方式
                | ControlStyles.ResizeRedraw//当控件大小发生变化时就重新绘制
                | ControlStyles.DoubleBuffer// 双缓冲
                | ControlStyles.SupportsTransparentBackColor//则控件接受 alpha 组件数小于 255 个的 BackColor 来模拟透明度
                | ControlStyles.AllPaintingInWmPaint//禁止擦除背景.则控件忽略窗口消息 WM_ERASEBKGND 以减少闪烁
                | ControlStyles.EnableNotifyMessage// 
                                                   // Enable the OnNotifyMessage event so we get a chance to filter out 
                                                   // Windows messages before they get to the form's WndProc
                | ControlStyles.OptimizedDoubleBuffer//则控件将首先绘制到缓冲区而不是直接绘制到屏幕，这可以减少闪烁
                , true);
            InitializeComponent();
            this.user = user;
            newItem.MarketID = marketID;
            modifyButton.Visible = false;
            deleteButton.Visible = false;
            Color userColor = MainForm.MainForm.userColor;
            UIStyles.InitColorful(userColor, Color.White);
        }

        public AddItem(User user, Item item)
        {
            SetStyle(
                ControlStyles.UserPaint//使用自定义的绘制方式
                | ControlStyles.ResizeRedraw//当控件大小发生变化时就重新绘制
                | ControlStyles.DoubleBuffer// 双缓冲
                | ControlStyles.SupportsTransparentBackColor//则控件接受 alpha 组件数小于 255 个的 BackColor 来模拟透明度
                | ControlStyles.AllPaintingInWmPaint//禁止擦除背景.则控件忽略窗口消息 WM_ERASEBKGND 以减少闪烁
                | ControlStyles.EnableNotifyMessage// 
                                                   // Enable the OnNotifyMessage event so we get a chance to filter out 
                                                   // Windows messages before they get to the form's WndProc
                | ControlStyles.OptimizedDoubleBuffer//则控件将首先绘制到缓冲区而不是直接绘制到屏幕，这可以减少闪烁
                , true);
            InitializeComponent();
            this.user = user;
            newItem = item;
            itemNameTextBox.Text = newItem.ItemName;
            countTextBox.Text = Convert.ToString(newItem.ItemCount);
            priceTextBox.Text = Convert.ToString(newItem.ItemPrice);
            uiAvatar1.Image = ImageService.BytesToImage(newItem.ItemPicture);
            addButton.Visible = false;
            this.Text = "查看商品详细";
        }

        private void uiAvatar1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string imagePath = openFileDialog.FileName;
                uiAvatar1.Image = Image.FromFile(imagePath);
                newItem.ItemPicture = ImageService.ImageToBytes(uiAvatar1.Image);
            }
        }

        // 判断是否有输入为空的现象
        public bool isNull()
        {
            if (itemNameTextBox.Text == "" || itemNameTextBox.Text == null)
            {
                UIMessageBox.Show("请输入商品名字");
                return false;
            }
            if (priceTextBox.Text == "" || priceTextBox.Text == null)
            {
                UIMessageBox.Show("请输入商品价格");
                return false;
            }
            if (countTextBox.Text == "" || countTextBox.Text == null)
            {
                UIMessageBox.Show("请输入商品数量");
                return false;
            }
            return true;
        }

        private void addButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (!isNull()) return;
                if (newItem.ItemPicture == null)
                {
                    UIMessageBox.Show("请上传商品照片");
                    return;
                }
                newItem.ItemID = Guid.NewGuid().ToString();
                newItem.ItemName = itemNameTextBox.Text;
                newItem.ItemPrice = Convert.ToDouble(priceTextBox.Text);
                newItem.ItemCount = Convert.ToInt32(countTextBox.Text);
                itemService.AddItem(newItem);
                UIMessageBox.ShowSuccess("增加成功！");
                this.Close();
            }
            catch //(DbUpdateException ex)
            {
                UIMessageBox.ShowError("增加失败，请稍后再试");
            }
        }

        private void priceTextBox_TextChanged(object sender, EventArgs e)
        {
            double ang;
            if (!double.TryParse(priceTextBox.Text, out ang) && priceTextBox.Text != "")
            {
                UIMessageBox.Show("请输入正确的价格");
                priceTextBox.Text = ""; //textbox 重置
            }
        }

        private void countTextBox_TextChanged(object sender, EventArgs e)
        {
            int ang;
            if (!int.TryParse(countTextBox.Text, out ang) && countTextBox.Text != "")
            {
                UIMessageBox.Show("请输入正确的数量");
                countTextBox.Text = ""; //textbox 重置
            }
        }

        private void modifyButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (!isNull()) return;
                newItem.ItemName = itemNameTextBox.Text;
                newItem.ItemPrice = Convert.ToDouble(priceTextBox.Text);
                newItem.ItemCount = Convert.ToInt32(countTextBox.Text);
                itemService.DeleteItem(newItem);
                itemService.AddItem(newItem);
                UIMessageBox.ShowSuccess("修改成功！");
                this.Close();
            }
            catch //(DbUpdateException ex)
            {
                UIMessageBox.ShowError("修改失败，请稍后再试");
            }
        }

        private void deleteButton_Click(object sender, EventArgs e)
        {
            try
            {
                itemService.DeleteItem(newItem);
                UIMessageBox.ShowSuccess("删除成功！");
                this.Close();
            }
            catch
            {
                UIMessageBox.ShowError("删除失败，请稍后再试");
            }
        }
    }
}
